<template>
  <div class="app-container">
    <!-- 所属实体 -->
    <div class="bean-container">
      <h3 class="entity-title">模型</h3>
      <el-row class="last_line">
        <el-col :span="12">
          <div class="cell-title">模块:</div>
          <div class="cell-content">{{entity.app_label}}</div>
        </el-col>
        <el-col :span="12" >
          <div class="cell-title">模型:</div>
          <div class="cell-content">{{entity.model}}</div>
        </el-col>
      </el-row>
    </div>
    <!-- 筛选 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="名称" style="width: 400px;" class="filter-item" @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div>

    <!-- 列表 -->
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;">
      <el-table-column :label="$t('table.id')" align="center" width="65">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称" min-width="150px">
        <template slot-scope="scope">
          <span class="link-type" @click="handleUpdate(scope.row)">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="权限code" min-width="150px">
        <template slot-scope="scope">
          <span class="link-type" @click="handleUpdate(scope.row)">{{ scope.row.codename }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" width="400" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <!-- <el-button type="primary" size="mini" @click="showPemission(scope.row)">权限</el-button> -->
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">{{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination v-show="total>0" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.limit" :total="total" background layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange" @current-change="handleCurrentChange"/>
    </div>

    <!-- 新增 或 修改 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="bean" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="名称" prop="name">
          <el-input v-model="bean.name"/>
        </el-form-item>
      </el-form>
      <el-form ref="dataForm" :rules="rules" :model="bean" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="权限" prop="codename">
          <el-input v-model="bean.codename"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogPvVisible" title="Reading statistics">
      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
        <el-table-column prop="key" label="Channel"/>
        <el-table-column prop="pv" label="Pv"/>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogPvVisible = false">{{ $t('table.confirm') }}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import permissionService from '@/api/permission'
import contentTypeService from '@/api/content_type'

import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'UserList',
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        // importance: undefined,
        name: '',
        contentTypeId: 0
        // type: undefined,
        // sort: '+id'
      },
      // 所属实体，这里就是content_type
      entity: {
        id: 0,
        app_label: '',
        model: ''
      },
      bean: {
        id: undefined,
        name: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        name: [{ required: true, message: 'name is required', trigger: 'blur' }]
      },
      downloadLoading: false,
      slideShow: false
    }
  },
  async created() {
    await this.getContentType()
    await this.getList()
  },
  methods: {
    async getContentType() {
      console.log(this.$route.params)
      this.entity = await contentTypeService.get(this.$route.params.content_type_id)
      this.listQuery.contentTypeId = this.entity.id
    },
    async getList() {
      const ret = await permissionService.getList(this.listQuery)
      this.list = ret.data.list
      this.total = ret.data.total
      setTimeout(() => {
        this.listLoading = false
      }, 1.5 * 1000)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.bean = {
        id: undefined,
        name: ''
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = Object.assign({}, this.bean)
          debugger
          data.contentTypeId = this.entity.id
          permissionService.create(data).then(res => {
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
            this.dialogFormVisible = false
            this.getList()
          })
        }
      })
    },
    handleUpdate(row) {
      console.log(row)
      this.bean = Object.assign({}, row) // copy obj
      this.bean.contentTypeId = this.entity.id
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = Object.assign({}, this.bean)
          permissionService.update(data).then(res => {
            if (res.code === 200) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.dialogFormVisible = false
              this.getList()
            }
          })
        }
      })
    },
    async handleDelete(row) {
      this.$alert('确定删除么?', '删除', {
        confirmButtonText: '确定',
        callback: action => {
          permissionService.delete(row.id).then(res => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
        }
      })
    }
  }
}
</script>
<style scoped>
.slideShow {
  transform: translateX(0)!important;
  transition: transform .2s, -webkit-transform .2s;
}
.bean-container {
  margin-bottom: 10px;
}
.entity-title {
  font-size: 14px;
}
.cell-title {
  font-size: 12px;
  color: #2c2c2c;
  display: inline-block;
}
.cell-content {
  font-size: 12px;
  color: #2c2c2c;
  display: inline-block;
}
.last_line {
  padding-bottom: 5px; 
  border-bottom: 1px solid #eeeeee;
}
</style>
